﻿using NAnt.Core;
using NAnt.Core.Attributes;
using System.IO;
using System;

namespace NAnt.SVN.UtilTasks
{
    /// <summary>
    /// Util task to create a temp directory.
    /// Run the createtempdir in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("createtempdir")]
    public class CreateTempDir : Task
    {
        private string mode = "guid";
        private bool prefixproject = false;
        
        private const string GUIDFORMAT = "D";
        private const string DATEFORMAT = "yyyyMMdd'-'HHmmss";

        #region Public fields

        /// <summary>
        /// The name of the property
        /// </summary>
        public const string TEMPPROPERTYNAME = "tempdir";

        /// <summary>
        /// Define the mode of the file creation accepting two 
        /// defined values: guid and date. Other values are
        /// regarded as custom
        /// </summary>
        [TaskAttribute("mode",Required=false)]
        [StringValidator(AllowEmpty=true)]
        public string ModeValue
        {
            get { return mode; }
            set { mode = value; }
        }

        /// <summary>
        /// Get the mode as enum value
        /// </summary>
        public TempDirMode Mode
        {
            get
            {
                if (ModeValue.Equals(TempDirModeConstants.GUID))
                    return TempDirMode.Guid;
                else if (ModeValue.Equals(TempDirModeConstants.DATE))
                    return TempDirMode.Date;
                else return TempDirMode.Custom;
            }
        }

        /// <summary>
        /// Define the mode of the file creation accepting two 
        /// defined values: guid and date. Other values are
        /// regarded as custom
        /// </summary>
        [TaskAttribute("prefixproject", Required = false)]
        [BooleanValidator()]
        public bool PrefixProjectName
        {
            get { return prefixproject; }
            set { prefixproject = value; }
        }

        #endregion

        /// <summary>
        /// Execute the task
        /// </summary>
        protected override void ExecuteTask()
        {
            //Check if the command has already run
            if (Properties[TEMPPROPERTYNAME] != null)
            {
                Log(Level.Warning, Resources.Strings.UTIL0306);
                return;
            }
            //Get a temp path
            Log(Level.Info, Project.ProjectName);
            string tempPath = Path.GetTempPath() + 
                (PrefixProjectName ? Project.ProjectName + "-" : "") + 
                getDirName() /*+ @"\"*/;
            //Create it
            if (!Directory.Exists(tempPath))
            {
                try
                {
                    Directory.CreateDirectory(tempPath);
                    Properties[TEMPPROPERTYNAME] = tempPath;
                    Log(Level.Info,
                    string.Format(Resources.Strings.UTIL0101, tempPath));
                }
                catch (Exception e)
                {
                    Log(Level.Error,
                    string.Format(Resources.Strings.UTIL0301, e.Message));
                }
            }
            else
            {
                Log(Level.Error, 
                    string.Format(Resources.Strings.UTIL0304, tempPath));
            }
        }

        /// <summary>
        /// Get the dirname
        /// </summary>
        /// <returns></returns>
        public string getDirName()
        {
            if (Mode == TempDirMode.Guid)
                return System.Guid.NewGuid().ToString(GUIDFORMAT);
            else if (Mode == TempDirMode.Date)
                return System.DateTime.Now.ToString(DATEFORMAT);
            else return ModeValue.ToLower();
        }
    }

    /// <summary>
    /// The enum of the modes
    /// </summary>
    public enum TempDirMode
    { 
        Guid = 1, Date, Custom
    }

    /// <summary>
    /// The string constants
    /// </summary>
    public class TempDirModeConstants
    {
        public static string GUID = "guid";
        public static string DATE = "date";
    }
}
